
#include "COutputStates.h"
#include "pugixml/src/pugixml.hpp"
#include <string>
#include <list>


using namespace std;


COutputStates* COutputStates::myInstance(NULL);
bool COutputStates::myInitialization(false);
bool COutputStates::myDataChanged(false);


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

COutputStates::COutputStates()
{
    myOutputStates.reset();
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

COutputStates* COutputStates::getInstance()
{
    if (NULL == myInstance)
    {
        myInstance = new COutputStates();
    }
    
    return myInstance;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

COutputStates::~COutputStates()
{
    myOutputStates.~xml_document();
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::init()
{
    if (false == myInitialization)
    {
        myInitialization = true;
        
        if (true == parseFile())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    return true; 
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::parseFile()
{
    pugi::xml_parse_result parseStatus = myOutputStates.load_file("outputStates.xml");
    
    if (pugi::status_ok == parseStatus)
    {
        return false;
    }

    return true;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::getOutputState(const string theOutputNumber, string &theState)
{
    theState = "";
    
    pugi::xml_node theNode = myOutputStates.child("outputstates").find_child_by_attribute("number", theOutputNumber.c_str()).child("state");

    if (NULL != theNode)
    {
        theState = theNode.child_value();

        return true;
    }

    return false;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::setOutputState(const string theOutputNumber, const string theState)
{
    pugi::xml_node theNode = myOutputStates.child("outputstates").find_child_by_attribute("number", theOutputNumber.c_str()).child("state");

    if (NULL != theNode)
    {
        if (true == theNode.first_child().set_value(theState.c_str()))
        {
            myDataChanged = true;
            
            return true;
        }
    }

    return false;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::getOutputBrightness(const string theOutputNumber, string &theBrightness)
{
    theBrightness = "";

    pugi::xml_node theNode = myOutputStates.child("outputstates").find_child_by_attribute("number", theOutputNumber.c_str()).child("brightness");

    if (NULL != theNode)
    {
        theBrightness = theNode.child_value();

        return true;
    }

    return false;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::setOutputBrightness(const string theOutputNumber, const string &theBrightness)
{
    pugi::xml_node theNode = myOutputStates.child("outputstates").find_child_by_attribute("number", theOutputNumber.c_str()).child("brightness");

    if (NULL != theNode)
    {
        if (true == theNode.first_child().set_value(theBrightness.c_str()))
        {
            myDataChanged = true;
            
            return true;
        }
    }

    return false;
}


// -----------------------------------------------------------------------------
// @see header
// -----------------------------------------------------------------------------

bool COutputStates::saveOutputStates()
{
    if (true == myDataChanged)
    {
        myDataChanged = false;
        
        return myOutputStates.save_file("saved_outputstates.xml");
    }
    else
    {
        return true;
    }
}
